//
//  PersonalViewController.m
//  Medgretraining
//
//  Created by 辰领科技 on 16/5/17.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "PersonalViewController.h"
#import "IntegralViewController.h"
#import "PersonInfoViewController.h"
@interface PersonalViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UIButton *personHead;
@property (weak, nonatomic) IBOutlet UILabel *personName;
@property (weak, nonatomic) IBOutlet UILabel *accountMoney;
@property(nonatomic,strong)NSString * headImage;
@end

@implementation PersonalViewController



- (void)viewWillAppear:(BOOL)animated{
    if([Common getLoginStatus]){
        [self getPersonalInfo];
        [self getAccountLeftMoney];
        
        _accountMoney.hidden = NO;
    }else{
        [_personHead setImage:[UIImage imageNamed:@"personal7"] forState:UIControlStateNormal];
        _personName.text = @"点击登录";
        _accountMoney.hidden = YES;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initViewLayout];
//    [self getPersonalInfo];
//    [self getAccountLeftMoney];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.scroView];
     _scroView.frame = CGRectMake(0, 0, 320* VerticalRatio(), 568* VerticalRatio()-49);
     _scroView.contentSize = flexibleSize(CGSizeMake(0, 568* VerticalRatio()-48), NO);
    
    _personHead.layer.masksToBounds = YES;
    _personHead.layer.cornerRadius = 27.5*VerticalRatio();
    

}


#pragma mark  ==== 查询用户信息
- (void)getPersonalInfo{
    if (![Common getLoginStatus]) {
       [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:HK_queryMemberInfoById withHttpType:@"POST" withTag:HK_HK_queryMemberInfoById];
}

#pragma mark === 查询账户余额
- (void)getAccountLeftMoney{
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:HK_queryMemberMuseEpurseInfoById withHttpType:@"POST" withTag:HK_HK_queryMemberMuseEpurseInfoById];
}

#pragma mark - 头像点击事件
- (IBAction)personInfo:(id)sender {
    
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }else{
    
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    PersonInfoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PersonInfo"];
        page.block = ^(){
            self.tabBarController.selectedIndex = 0;
        };
    [[Common getNavigation]pushViewController:page];
    }
}



#pragma mark - 我的积分
- (IBAction)integral:(id *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    IntegralViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Integral"];
    page.headImage = _headImage;
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 我的评论
- (IBAction)Comments:(id)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Comments"];
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 我的收藏
- (IBAction)collection:(id)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"collection"];
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 修改密码
- (IBAction)ChangePassword:(id)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ChangePassword"];
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 更多
- (IBAction)more:(id)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"More"];
    [[Common getNavigation]pushViewController:page];
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==HK_HK_queryMemberInfoById) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            NSDictionary * dic = database[@"result"];
            NSString * image = [NSString stringWithFormat:@"%@",dic[@"museImage"]];
            _headImage = image;
            if (image && image.length>0 && ![dic[@"museImage"] isEqual:[NSNull null]]) {
                [_personHead sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]] forState:UIControlStateNormal];
                
            }
            if ([dic[@"museNickName"] isEqual:[NSNull null]]) {
                _personName.text = @"";
            }else{
                _personName.text = [NSString stringWithFormat:@"%@",dic[@"museNickName"]];
            } 
        }
    }else if (tag == HK_HK_queryMemberMuseEpurseInfoById){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
             _accountMoney.text = [NSString stringWithFormat:@"可以余额：￥%.2f",[database[@"result"][@"museEpurse"]floatValue]];
        }
    }

    
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
        
        //
        [self dismissWaitDialog];
        
        
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    

@end
